#include <vector>
#include "buffer_node.h"
#define private public
#include "buffer_node_pool.h"
#undef private
#include "gtest/gtest.h"

namespace Base {
namespace SystemLog {
namespace {
constexpr size_t TEST_SINGLE_BUFFER_SIZE = 1024; // 1KB
constexpr size_t TEST_MAX_BUFFER_NODES = 5;
} // namespace

class BufferNodePoolSecurityTest : public testing::Test {
public:
    static void SetUpTestCase(void) {}
    static void TearDownTestCase(void) {}
    void SetUp() {}
    void TearDown() {}
};

/**
 * @tc.name: BufferNodePoolSecurityTest_001
 * @tc.desc: memory leak detection
 * @tc.type: SECURITY
 */
TEST_F(BufferNodePoolSecurityTest, BufferNodePoolSecurityTest_001)
{
    BufferNodePool<> pool;
    auto node = pool.Allocate();
    ASSERT_NE(node, nullptr);
    // 不释放节点，测试内存泄漏
}

/**
 * @tc.name: BufferNodePoolSecurityTest_002
 * @tc.desc: boundary conditions testing
 * @tc.type: SECURITY
 */
TEST_F(BufferNodePoolSecurityTest, BufferNodePoolSecurityTest_002)
{
    BufferNodePool<TEST_SINGLE_BUFFER_SIZE> pool(TEST_MAX_BUFFER_NODES);

    std::vector<BufferNode<TEST_SINGLE_BUFFER_SIZE>*> nodes;
    for (size_t i = 0; i < TEST_MAX_BUFFER_NODES; ++i) {
        auto node = pool.Allocate();
        ASSERT_NE(node, nullptr);
        nodes.push_back(node);
    }
    auto node = pool.Allocate();
    EXPECT_EQ(node, nullptr);

    for (auto n : nodes) {
        pool.Deallocate(n);
    }
    node = pool.Allocate();
    EXPECT_NE(node, nullptr);
    pool.Deallocate(node);
}

} // namespace SystemLog
} // namespace Base
